package com.learning.servlet2x.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * ClassName: ObjectAttributeListener
 * Description: 监听对象属性变化，如添加、更新、删除
 * 一次性实现 三个接口
 * Date: 2014-12-10 22:25 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class ObjectAttributeListener implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {


    @Override
    public void attributeAdded(ServletContextAttributeEvent scab) {

    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scab) {

    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scab) {

    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {

    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {

    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {

    }

    /**
     * 添加属性
     *
     * @param se
     */
    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        HttpSession session = se.getSession();
        System.out.println("新建session属性：" + se.getName() + ", 值为：" + se.getValue());
    }

    /**
     * 删除属性
     *
     * @param se
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        HttpSession session = se.getSession();
        System.out.println("删除session属性：" + se.getName() + ", 值为：" + se.getValue());
    }

    /**
     * 修改属性
     */
    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        HttpSession session = se.getSession();
        String name = se.getName();
        Object oldValue = se.getValue();
        System.out.println("修改session属性：" + name + ", 原值：" + oldValue + ", 新值：" + session.getAttribute(name));
    }
}
